﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace DBTool
{
    public static class SVNHelper
    {       
        public static void ExecuteSvnCommand( SVNInfo svnInfo, string cmd )
        {
            string svnExecutable = Path.Combine(svnInfo._svnPath, "svn.exe");

            if( false == File.Exists( svnExecutable ) )
            {
                MessageBox.Show( "SVN.exe Not Exist. Path : " + svnExecutable, "SVN PATH", MessageBoxButtons.OK, MessageBoxIcon.Error );
                return;
            }

            //cmd += " --username " + svnInfo._svnUserName + " --password " + svnInfo._svnPassword;

            Process process = PrepareSVNProcess(cmd, svnExecutable);
            
            try
            {
                process.Start();           
            }
            catch( Exception ex )
            {
                MessageBox.Show(ex.Message);
            }

            process.WaitForExit();

            process.Close();

        }

        public static Process PrepareSVNProcess(string arg, string cmd)
        {
            Process process = new Process();

            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;

            process.StartInfo.Arguments = arg;
            process.StartInfo.FileName = cmd;

            return process;
        }
         
   
        /*
        string line = null;
        //Write process output to log file
        while ((line = process.StandardOutput.ReadLine()) != null)
        {            
            WriteLogLine( line );
        }
        */

        public static void WriteLogLine(string logText)
        {
            string logFolder = Path.Combine( "C:\\Work", "Log" );
            if ( ! Directory.Exists(logFolder) )
                Directory.CreateDirectory( logFolder );

            string logFile = Path.Combine( logFolder, ".txt" );
            string logLine = 
	        String.Format( "[{0}] {1}\r\n", DateTime.Now.ToString(), logText );

            File.AppendAllText( logFile, logLine , Encoding.UTF8);

        }//WriteLogLine

    }
}
